package com.heima.shopweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.shopweb.mapper.CartMapper;
import com.heima.shopweb.model.Cart;
import com.heima.shopweb.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    private CartMapper cartMapper;

    @Autowired
    public void setCartMapper(CartMapper cartMapper) {
        this.cartMapper = cartMapper;
    }

    @Override
    public List<Cart> findByMemberId(Integer memberId) {
        return cartMapper.findByMemberId(memberId);

    }

    @Override
    public boolean update(Cart cart) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getId, cart.getId());
        return cartMapper.update(cart, queryWrapper) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return cartMapper.deleteById(id) > 0;
    }

    @Override
    public boolean insert(Cart cart) {
        return cartMapper.insert(cart) > 0;
    }

    @Override
    public Cart findByCart(Cart cart) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getMemberId, cart.getMemberId());
        queryWrapper.eq(Cart::getGoodId, cart.getGoodId());
        return cartMapper.selectOne(queryWrapper);
    }
}